package com.atguigu1.core.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.{DataFrame, SparkSession}

/**
 * @description: udf
 * @time: 2021/3/25 11:28
 * @author: baojinlong
 **/
object SparkSqlDemo02 {
  def main(args: Array[String]): Unit = {
    // 创建SparkSql的运行环境
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("sparkSql")
    val spark: SparkSession = SparkSession.builder.config(sparkConf).getOrCreate

    val dataFrame: DataFrame = spark.read.json("datas/user.json")
    dataFrame.createOrReplaceTempView("user")
    // spark.sql("select age,username from user").show
    // 注册函数
    spark.udf.register("prefixName", (name: String) => "Name:" + name)
    spark.sql("select age,prefixName(username) from user").show
    // 关闭环境
    spark.close
  }


}
